package com.project.service.Imp;

import com.project.bean.RecordBean;
import com.project.mapper.IRecordMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRecordService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RecordService extends BaseService implements IRecordService {
    @Override
    public List<RecordBean> findByRoomId(Integer id) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        List<RecordBean> byRoomID = mapper.findByRoomId(id);
        session.close();
        return byRoomID;
    }

    @Override
    public void addRecord(String record, Integer roomId) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        mapper.addRecord(record, roomId);
        mapper.updateStatus(roomId);
        session.commit();
        session.close();
    }


    @Override
    public void updateRecord(Integer recordId, Integer roomId) {
        SqlSession session = this.getSession();
        IRecordMapper mapper = session.getMapper(IRecordMapper.class);
        mapper.updateRecord(recordId, roomId);
        session.commit();
        List<RecordBean> ro = mapper.findByRoomIdStatus(roomId);
        System.out.println(ro.size());
        if (ro.size() == 0) {
            mapper.upRoomStatus(roomId);
            session.commit();
        }
    }
}
